package com.ruoyi.shebei.controller;

import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shebei.domain.Shebei;
import com.ruoyi.shebei.service.IShebeiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备Controller
 * 
 * @author admin
 * @date 2024-07-26
 */
@RestController
@RequestMapping("/shebei/shebei")
public class ShebeiController extends BaseController
{
    @Autowired
    private IShebeiService shebeiService;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('shebei:shebei:list')")
    @GetMapping("/list")
    public TableDataInfo list(Shebei shebei)
    {
        startPage();
        List<Shebei> list = shebeiService.selectShebeiList(shebei);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @PreAuthorize("@ss.hasPermi('shebei:shebei:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Shebei shebei)
    {
        List<Shebei> list = shebeiService.selectShebeiList(shebei);
        ExcelUtil<Shebei> util = new ExcelUtil<Shebei>(Shebei.class);
        util.exportExcel(response, list, "设备数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Shebei> util = new ExcelUtil<Shebei>(Shebei.class);
        util.importTemplateExcel(response, "设备数据");
    }
    /**
     * 导入数据
     */
    @Log(title = "设备", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('shebei:shebei:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Shebei> util = new ExcelUtil<Shebei>(Shebei.class);
        InputStream inputStream = file.getInputStream();
        List<Shebei> list = util.importExcel(inputStream );
        inputStream.close();
        int count = shebeiService.batchInsertShebei(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }


    /**
     * 获取设备详细信息
     */
    @PreAuthorize("@ss.hasPermi('shebei:shebei:query')")
    @GetMapping(value = "/{shebeiId}")
    public AjaxResult getInfo(@PathVariable("shebeiId") Long shebeiId)
    {
        return success(shebeiService.selectShebeiByShebeiId(shebeiId));
    }

    /**
     * 新增设备
     */
    @PreAuthorize("@ss.hasPermi('shebei:shebei:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Shebei shebei)
    {
        return toAjax(shebeiService.insertShebei(shebei));
    }

    /**
     * 修改设备
     */
    @PreAuthorize("@ss.hasPermi('shebei:shebei:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Shebei shebei)
    {
        return toAjax(shebeiService.updateShebei(shebei));
    }

    /**
     * 删除设备
     */
    @PreAuthorize("@ss.hasPermi('shebei:shebei:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shebeiIds}")
    public AjaxResult remove(@PathVariable Long[] shebeiIds)
    {
        return toAjax(shebeiService.deleteShebeiByShebeiIds(shebeiIds));
    }


    @GetMapping("/count")
    public int count(Long shebeiId){
    int count = shebeiService.selectcount(shebeiId);
       return count;
    }
}
